package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.merchant.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.merchant.service.MerchantOrderService;
import com.qiguliuxing.dts.merchant.support.AuthSupport;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/merchant/order")
@Validated
@Api(tags = ApiAdminConsts.Order.PARENT)
public class MerchantOrderController {
	private static final Logger logger = LoggerFactory.getLogger(MerchantOrderController.class);

	@Autowired
	private MerchantOrderService merchantOrderService;

	/**
	 * 查询订单
	 *
	 * @param userId
	 * @param orderSn
	 * @param orderStatusArray
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
    @GetMapping("/list")
	@RequiresPermissions("merchant:order:list")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "查询")
	@ApiOperation(value = ApiAdminConsts.Order.LIST)
	@WebLog(description = ApiAdminConsts.Order.LIST,excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "用户id") Integer userId,
					   @ApiParam(value = "订单编号") String orderSn,
					   @ApiParam(value = "提货码") String fetchCode,
					   @ApiParam(value = "订单状态") @RequestParam(required = false) List<Short> orderStatusArray,
					   @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
					   @ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
					   @ApiParam(value = "排序规则",defaultValue = "desc")  @Order @RequestParam(defaultValue = "desc") String order) {
		return merchantOrderService.list(userId, orderSn,fetchCode,orderStatusArray, page, limit, sort, order);
	}

	/**
	 * 订单详情
	 *
	 * @param id
	 * @return
	 */
    @GetMapping("/detail")
	@RequiresPermissions("merchant:order:read")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "详情")
	@ApiOperation(value = ApiAdminConsts.Order.READ)
	@WebLog(description = ApiAdminConsts.Order.READ)
	public Object detail( @ApiParam(value = "订单id") @NotNull Integer id) {
		return merchantOrderService.detail(id);
	}

	/**
	 * 订单退款
	 *
	 * @param body 订单信息，{ orderId：xxx }
	 * @return 订单退款操作结果
	 */
    @PostMapping("/refund")
	@RequiresPermissions("merchant:order:refund")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "订单退款")
	@ApiOperation(value = ApiAdminConsts.Order.REFUND)
	@WebLog(description = ApiAdminConsts.Order.REFUND, persistence = true)
	public Object refund(@RequestBody String body) {
		return merchantOrderService.refund(body);
	}

	/**
	 * 订单退款
	 *
	 * @param id 订单信息，{ orderId：xxx }
	 * @return 订单退款操作结果
	 */
	@GetMapping("/detailRefundOrder")
	@ApiOperation(value = ApiAdminConsts.Order.DETAIL_REFUND)
	@WebLog(description = ApiAdminConsts.Order.DETAIL_REFUND,persistence = true)
	public Object detailRefundOrder( @ApiParam(value = "订单id")  @NotNull Integer id) {
		return merchantOrderService.detailRefundOrder(id);
	}

	/**
	 * 提货确认操作
	 *
	 * @param body
	 *            订单信息，{ orderId：xxx }
	 * @return 订单操作结果
	 */
    @PostMapping("/confirm")
	@RequiresPermissions("merchant:order:confirm")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "提货确认")
	@ApiOperation(value = ApiAdminConsts.Order.CONFIRM)
	@WebLog(description = ApiAdminConsts.Order.CONFIRM,persistence = true)
	public Object confirm(@RequestBody String body) {
		return merchantOrderService.confirm(body);
	}


	/**
	 * 发货
	 *
	 * @param body 订单信息，{ orderId：xxx, shipSn: xxx, shipChannel: xxx }
	 * @return 订单操作结果
	 */
    @PostMapping("/ship")
	@RequiresPermissions("merchant:order:ship")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "订单发货")
	@ApiOperation(value = ApiAdminConsts.Order.SHIP)
	@WebLog(description = ApiAdminConsts.Order.SHIP,persistence = true)
	public Object ship(@RequestBody String body) {
		return merchantOrderService.ship(body);
	}

	/**
	 * 订单商品评论回复
	 *
	 * @param body 订单信息，{ orderId：xxx }
	 * @return 订单操作结果
	 */
    @PostMapping("/reply")
	@RequiresPermissions("merchant:order:reply")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "订单商品回复")
	@ApiOperation(value = ApiAdminConsts.Order.REPLY)
	@WebLog(description = ApiAdminConsts.Order.REPLY,persistence = true)
	public Object reply(@RequestBody String body) {
		return merchantOrderService.reply(AuthSupport.adminId(),body);
	}

	/**
	 * 快递信息加载
	 */
    @GetMapping("/listShipChannel")
	@RequiresPermissions("merchant:order:listShip")
	@RequiresPermissionsDesc(menu = { "商场管理", "订单管理" }, button = "快递加载")
	@ApiOperation(value = ApiAdminConsts.Order.LIST_SHIP)
	@WebLog(description = ApiAdminConsts.Order.LIST_SHIP,excludes = {LogParamsConsts.RESPONSE})
	public Object listShipChannel() {
		return merchantOrderService.listShipChannel();
	}
}
